/**
 *       快捷 列表 ,  不建议 复杂 ITEM 使用
 */

//////////////////////////// init 实例
// {
//     data: ds,
//     init(item: QuickList_item, data, index: number) {
//     },
//     button_click(item: QuickList_item, data, onBtn_name: string, target: cc.Button, index: number){        
//     }
// }


import BaseUI from "../BaseUI";
import QuickList_item from "./QuickList_item";

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu("扩展组件/QuickList")
export default class QuickList extends BaseUI {
    init_data(d: object): void {

    }

    @property(cc.Node)
    item: cc.Node = null;

    qitem: cc.Node = null; ///// 实际使用 ITEM 对象

    @property
    first_init_num = 10; ////////// 第一次初始化 数量

    baseUI_onLoad(): void {
        this.find_item();
    }

    private find_item() {
        if (this.item) {
            this.init_qitem(this.item);
        } else if (this.node.getChildByName("item")) {
            this.init_qitem(this.node.getChildByName("item"));
        }

        if (this.qitem) { ////////// ITEM  初始化成功
            
        } else { /////// ITEM 初始化 失败
            console.error("QuickList中ITEM未找到!");
        }
    }

    private init_qitem(n: cc.Node) {
        if (n.getComponent(BaseUI)) {
            console.error("QuickList中ITEM已存在baseUI组件!");
            return;
        }
        this.qitem = n;
        n.removeFromParent();

        let bs: cc.Button[] = n.getComponentsInChildren(cc.Button);
        bs.forEach((b: cc.Button) => {
            b.clickEvents.length = 0;
        });
    }

    baseUI_onDestroy(): void {

    }
    onBtn_click(target: cc.Button, onBtn_name: string): number | void {

    }

    start() {

    }

    item_interface: Quicklist_event<any> = null;
    init<T>(init: Quicklist_event<T>) {
        if (this.qitem == null) {
            // console.error("QuickList中ITEM未找到!");
            this.find_item();
            // return;
        }

        this.item_interface = init;

        if (!this.item_interface.is_auto_label) {
            this.item_interface.is_auto_label = true;
        }
        if (!this.item_interface.is_auto_urlSprite) {
            this.item_interface.is_auto_urlSprite = true;
        }

        if (init.data) {
            this.set_data<T>(init.data);
        }
    }

    set_data<T>(ds: T[]) {
        this.clear_data();

        let len = Math.min(this.first_init_num, ds.length);

        for (let i = 0; i < len; i++) {
            let item: cc.Node = cc.instantiate(this.qitem);
            this.node.addChild(item);
            this._init_item(item, ds[i], i);
        }

        this.index_uninit = len;
        if (ds.length > this.first_init_num) {
            this.uninit_data = ds.filter((v: T, i: number) => { return i >= this.first_init_num; });
        }
    }

    add_data<T>(ds: T[]) {
        // for (let i = 0; i < ds.length; i++) {
        //     let item: cc.Node = cc.instantiate(this.qitem);
        //     this.node.addChild(item);
        //     this._init_item(item, ds[i], i);
        // }

        this.uninit_data.push(...ds);
    }

    private _init_item(item: cc.Node, d: any, i: number) {
        let c: QuickList_item = item.addComponent(QuickList_item);
        c.set_data(d, this.item_interface, i); //////设置节点数据

        if (this.item_interface.rename) { ///// 改名
            item.name = this.item_interface.rename + i;
        }
        this.item_interface.init(c, d, i);
    }

    clear_data() {
        this.node.removeAllChildren();

        this.uninit_data = [];
        this.index_uninit = 0;
    }

    uninit_data: any[] = [];
    index_uninit: number = 0;
    protected update(dt: number): void {
        if (this.uninit_data.length > 0) {
            let item: cc.Node = cc.instantiate(this.qitem);
            this.node.addChild(item);
            this._init_item(item, this.uninit_data.shift(), this.index_uninit);
            this.index_uninit++;
        }
    }

    get_all_item(): QuickList_item[] {
        return this.node.getComponentsInChildren(QuickList_item);
    }
}

/**
 *       快捷 列表接口
 */
export interface Quicklist_event<T> {
    data: T[];
    init(item: QuickList_item, data: T, index: number): void;
    button_click(item: QuickList_item, data: T, onBtn_name: string, target: cc.Button, index: number): number | void;
    is_auto_label?: boolean; //// 是否 自动填写 label
    is_auto_urlSprite?: boolean;  ///// 是否 自动填写 urlSprite
    rename?: string; ////// 节点是否 改名
}

